package com.example.miaosha.miaosha_20220302.service.impl;

import com.example.miaosha.miaosha_20220302.entity.Detail;
import com.example.miaosha.miaosha_20220302.entity.Sku;
import com.example.miaosha.miaosha_20220302.service.DetailService;
import com.example.miaosha.miaosha_20220302.service.MiaoshaService;
import com.example.miaosha.miaosha_20220302.service.SkuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.Random;

/**
 * @author chenxuanwen
 * @Date: 2022/3/3  22:02
 */
@Service
public class MiaoshaServiceImpl implements MiaoshaService {

    @Autowired
    private SkuService skuService;

    @Autowired
    private DetailService detailService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object doMiaosha(Integer reduceNum, Integer skuId) {
        Sku sku = skuService.reduceNum(reduceNum,skuId);
        Detail detail = new Detail();
        detail.setQuantityTrade(reduceNum);
        detail.setSkuId(skuId);
        int i = new Random().nextInt(65552);
        detail.setOrderDetailId(i);
        detailService.save(detail);
        return sku;
    }
}
